import time
import sys
import atexit
import signal
from loguru import logger

handler_id = logger.add(
    "/tmp/test.log",
    # enqueue=True,
    buffering=4096,
    format="{time:YYYYMMDD-HH:mm:ss.S} {level} {message}",
)


def flush_logs():
    logger.info("接受到信号,正在关闭日志处理器")
    logger.remove()
    sys.exit(1)


# 注册退出处理
# atexit.register(flush_logs)
signal.signal(signal.SIGTERM, lambda *_: flush_logs())  # 处理 kill 默认信号


count = 20
while count > 0:
    logger.debug(f"this is test msg {count}")
    time.sleep(1)
    count -= 1
